package com.rssreader.client.forms;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.rssreader.client.RSSReaderConstants;
import com.rssreader.client.mvc.events.AppEvents;
import com.rssreader.client.services.FeedServiceAsync;
import com.rssreader.shared.model.Feed;

public class FeedForm extends FormPanel {

    private final TextField<String> tfTitle = new TextField<String>();
    private final TextArea taDescription = new TextArea();
    private final TextField<String> tfLink = new TextField<String>();
    private final TextField<String> tfImg = new TextField<String>();

    public FeedForm() {
	setHeaderVisible(false);
    }

    @Override
    protected void onRender(Element parent, int pos) {
	super.onRender(parent, pos);

	tfTitle.setFieldLabel("Title");
	tfTitle.setAllowBlank(false);
	tfTitle.getMessages().setBlankText("Title is required");

	taDescription.setFieldLabel("Description");
	taDescription.setAllowBlank(false);
	taDescription.getMessages().setBlankText("Description is required");

	tfLink.setFieldLabel("Link");
	tfLink.setAllowBlank(false);
	tfLink.setRegex("^http\\://[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?{1}");
	tfLink.getMessages().setBlankText("Link is required");
	tfLink.getMessages().setRegexText(
		"The link field must be a URL e.g. http://www.example.com/rss.xml");

	tfImg.setFieldLabel("Image");
	tfImg.setAllowBlank(true);

	add(tfTitle);
	add(taDescription);
	add(tfLink);
	add(tfImg);
    }

    public void save(final Feed feed) {
	feed.setTitle(tfTitle.getValue());
	feed.setDescription(taDescription.getValue());
	feed.setLink(tfLink.getValue());
	feed.setImageUrl(tfImg.getValue());

	final FeedServiceAsync feedService = Registry
		.get(RSSReaderConstants.FEED_SERVICE);
	feedService.saveFeed(feed, new AsyncCallback<Void>() {
	    @Override
	    public void onFailure(Throwable caught) {
		Info.display("RSS Reader", "Failed to save feed: " + feed.getTitle());
	    }

	    @Override
	    public void onSuccess(Void result) {
		Info.display("RSS Reader", "Feed " + feed.getTitle()
			+ " saved successfully");

		Dispatcher.forwardEvent(AppEvents.FeedAdded);

		// // retrieve the feed store from the Registry
		// final ListStore<BeanModel> feedStore = Registry
		// .get(RSSReaderConstants.FEED_STORE);
		// // retrieve the BeanModelFactory for the Feed class
		// BeanModelFactory beanModelFactory = BeanModelLookup.get()
		// .getFactory(feed.getClass());
		// // use the BeanModelFactory to create a BeanModel
		// representation
		// // of the Feed object and then add it to the feed store
		// feedStore.add(beanModelFactory.createModel(feed));
	    }
	});
    }
}
